//
//  AddManagerEditCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/7/18.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "AddManagerEditCon.h"
#import "GFAddressPicker.h"
#import "GFAddressPicker.h"
@interface AddManagerEditCon ()<UITextViewDelegate,GFAddressPickerDelegate,UITextFieldDelegate>

@property (nonatomic, strong) GFAddressPicker *pickerView;

@property(nonatomic,strong)MineButton *seleAddbtn;

@property(nonatomic,strong)UITextField *phonefield;
@property(nonatomic,strong)UITextField *namefield;
@property(nonatomic,strong)NSString *seleaddStr;
@property(nonatomic,strong)UITextView *detailaddtextview;
@property(nonatomic,strong)NSString *defaulStr;


@end

@implementation AddManagerEditCon

- (void)viewDidLoad {
    [super viewDidLoad];
    self.defaulStr = @"0";
    self.seleaddStr = @"";
    self.view.backgroundColor = LineBackColor;
    [self creatBackViewMethod];
    if (self.addressDic) {
        self.namefield.text = self.addressDic[@"ADDR_REALNAME"];
        self.phonefield.text = self.addressDic[@"ADDR_PHONE"];
        [self.seleAddbtn setTitle:self.addressDic[@"ADDR_CITY"] forState:UIControlStateNormal];
        self.seleaddStr = self.addressDic[@"ADDR_CITY"];
        self.detailaddtextview.text = self.addressDic[@"ADDR_DETAILS"];
        UIImageView *imagevi = [self.view viewWithTag:289];
        if ([self.addressDic[@"IS_DEFAULT"] integerValue] == 0) {
            imagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
            self.defaulStr = @"0";
        }else{
            imagevi.image = [UIImage imageNamed:@"LoginSelect"];
            self.defaulStr = @"1";
        }
    }
   
}

-(void)creatBackViewMethod{
    NSArray *arr = @[@"收货人",@"手机号"];
    for (int i = 0; i < 4; i ++) {
        UIView *bacview = [UIView new];
        bacview.backgroundColor = [UIColor whiteColor];
        bacview.layer.masksToBounds = YES;
        bacview.layer.cornerRadius = 7*kJLXWidthScale;
        bacview.layer.borderColor = LineColor.CGColor;
        bacview.layer.borderWidth = 1.0;
        [self.view addSubview:bacview];
        if (i < 3) {
            bacview.sd_layout
            .topSpaceToView(self.view, 20*kJLXWidthScale + 55*kJLXWidthScale *i)
            .leftSpaceToView(self.view, 8*kJLXWidthScale)
            .rightSpaceToView(self.view, 8*kJLXWidthScale)
            .heightIs(45*kJLXWidthScale);
            if (i < 2) {
                UITextField *field = [UITextField new];
                if (i == 0) {
                    self.namefield = field;
                }else{
                    self.phonefield = field;
                    self.phonefield.delegate = self;
                }
                field.tag = 300 + i;
                field.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
                field.textAlignment = NSTextAlignmentLeft;
                [bacview addSubview:field];
                field.sd_layout
                .centerYEqualToView(bacview)
                .leftSpaceToView(bacview, 8*kJLXWidthScale)
                .rightSpaceToView(bacview, 8*kJLXWidthScale)
                .heightIs(45*kJLXWidthScale);
                field.placeholder = arr[i];
            }else{
                MineButton *btn = [MineButton buttonWithType:UIButtonTypeSystem];
                self.seleAddbtn = btn;
                [btn setTitle:@"选择地址" forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(addressButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [bacview addSubview:btn];
                btn.sd_layout
                .centerYEqualToView(bacview)
                .leftSpaceToView(bacview, 8*kJLXWidthScale)
                .rightSpaceToView(bacview, 8*kJLXWidthScale)
                .heightIs(45*kJLXWidthScale);
            }
        }else{
            bacview.sd_layout
            .topSpaceToView(self.view, 20*kJLXWidthScale + 55*kJLXWidthScale *i)
            .leftSpaceToView(self.view, 8*kJLXWidthScale)
            .rightSpaceToView(self.view, 8*kJLXWidthScale)
            .heightIs(60*kJLXWidthScale);
            
            UITextView *field = [UITextView new];
            self.detailaddtextview = field;
            field.delegate = self;
            field.text = @"详细地址";
            field.textAlignment = NSTextAlignmentLeft;
            field.backgroundColor= [UIColor whiteColor];
            field.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
            field.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
            [bacview addSubview:field];
            field.sd_layout
            .centerYEqualToView(bacview)
            .leftSpaceToView(bacview, 10*kJLXWidthScale)
            .rightSpaceToView(bacview, 10*kJLXWidthScale)
            .heightIs(60*kJLXWidthScale);
        }
    }
    
    UIButton *selectbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectbtn addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:selectbtn];
    selectbtn.sd_layout
    .topSpaceToView(self.view, 30*kJLXWidthScale + 60*kJLXWidthScale*4)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale)
    .widthIs(180*kJLXWidthScale);
    
     UIImageView *imagevi = [UIImageView new];
    imagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
    imagevi.tag = 289;
    [selectbtn addSubview:imagevi];
    imagevi.sd_layout
    .centerYEqualToView(selectbtn)
    .leftSpaceToView(selectbtn,8*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale)
    .widthIs(20*kJLXWidthScale);
    
    UILabel *selelab = [UILabel new];
    selelab.text = @"设为默认地址";
    selelab.textColor = [UIColor blackColor];
    selelab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    [selectbtn addSubview:selelab];
    selelab.sd_layout
    .centerYEqualToView(selectbtn)
    .leftSpaceToView(imagevi, 5*kJLXWidthScale)
    .rightSpaceToView(selectbtn, 0)
    .heightIs(20*kJLXWidthScale);
    
    
    
    UIButton *savebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    savebutton.backgroundColor = [UIColor orangeColor];
    savebutton.layer.masksToBounds = YES;
    savebutton.layer.cornerRadius = 5*kJLXWidthScale;
    [savebutton setTitle:@"保存" forState:UIControlStateNormal];
    savebutton.titleLabel.font = [UIFont systemFontOfSize:17*kJLXWidthScale];
    [savebutton addTarget:self action:@selector(saveButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:savebutton];
    savebutton.sd_layout
    .topSpaceToView(self.view, 20*kJLXWidthScale + 60*kJLXWidthScale*5)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .rightSpaceToView(self.view, 20*kJLXWidthScale)
    .heightIs(45*kJLXWidthScale);
    
    
}

//已经取消编辑状态 结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text.length > 0) {
        if (![MethodClass utilityCheckPhoneNumInput:textField.text]) {
            [self alerWithMessage:@"手机号格式错误"];
            self.phonefield.text = @"";
        }
    }
    
    
}



-(void)addressButtonAction:(MineButton *)sender{
    self.pickerView = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.pickerView updateAddressAtProvince:@"河南省" city:@"郑州市" town:@"金水区"];
    self.pickerView.delegate = self;
    self.pickerView.font = [UIFont boldSystemFontOfSize:18];
    [self.view.window addSubview:self.pickerView];
    
}


-(void)selectButtonAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    UIImageView *imagevi = [sender viewWithTag:289];
    if (sender.selected) {
        imagevi.image = [UIImage imageNamed:@"LoginSelect"];
        self.defaulStr = @"1";
    }else{
         imagevi.image = [UIImage imageNamed:@"LoginNoSelect"];
         self.defaulStr = @"0";
    }
}
-(void)saveButtonAction:(UIButton *)sender{
    //保存
    /*
    IS_DEFAULT  是否默认地址 值 0 不是默认 ,1 默认
    USER_ID   用户ID 必须
    ADDR_PHONE 手机号 必填，检验格式
    ADDR_REALNAME 收货人姓名  必填
    ADDR_DETAILS  详细地址 必填
    ADDR_CITY  选择地址 必填
*/
    if (self.phonefield.text.length > 0 && self.namefield.text.length > 0 && self.seleaddStr.length > 0 && self.detailaddtextview.text.length > 0) {
        NSString *urlstr;
        if (self.addressDic) {
             urlstr  = [NSString stringWithFormat:@"/dashop/address/app/eidt.do?IS_DEFAULT=%@&USER_ID=%@&ADDR_PHONE=%@&ADDR_REALNAME=%@&ADDR_DETAILS=%@&ADDR_CITY=%@&ADDRESS_ID=%@",self.defaulStr,[UserDefault objectForKey:LogUserID],self.phonefield.text,self.namefield.text,self.detailaddtextview.text,self.seleaddStr,self.addressDic[@"ADDRESS_ID"]];
        }else{
         urlstr  = [NSString stringWithFormat:@"/dashop/address/app/save.do?IS_DEFAULT=%@&USER_ID=%@&ADDR_PHONE=%@&ADDR_REALNAME=%@&ADDR_DETAILS=%@&ADDR_CITY=%@",self.defaulStr,[UserDefault objectForKey:LogUserID],self.phonefield.text,self.namefield.text,self.detailaddtextview.text,self.seleaddStr];
        }
        NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
        NSString  *newUrlString = [endstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [self showDataMethodWithUrl:newUrlString];
    }else{
        [self alerWithMessage:@"地址信息填写不完整"];
    }
}

-(void)showDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}



- (void)GFAddressPickerCancleAction
{
    [self.pickerView removeFromSuperview];
}

- (void)GFAddressPickerWithProvince:(NSString *)province
                               city:(NSString *)city area:(NSString *)area
{
    [self.pickerView removeFromSuperview];
    
    NSString *addressstr = [NSString stringWithFormat:@"%@  %@  %@",province,city,area];
    [self.seleAddbtn setTitle:addressstr forState:UIControlStateNormal];
    self.seleaddStr = addressstr;
    
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSString *url;
    url = @"详细地址";
    if(textView.text.length < 1){
        textView.text = url;
        textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSString *url;
    url = @"详细地址";
    if([textView.text isEqualToString:url]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}

//封装提示框方法
-(void)alerWithMessage:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延迟1秒自动消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

@end
